namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class TenantFormControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantFormController>()
                .DerivesFrom<TenantController>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS, POST")
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentLocationAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantFormController());
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TenantFormController() as IRegisterRoutes).Register(null));
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();
            (new TenantFormController() as IRegisterRoutes).Register(routes);

            Assert.IsType<TenantRoute<TenantForm>>(routes[0]);
        }

        [Fact]
        public void op_HtmlRepresentation_CultureInfoNull()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/sample/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.Throws<ArgumentNullException>(() => new TenantFormController().HtmlRepresentation(null as CultureInfo));

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_HtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/sample/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsAssignableFrom<ViewResult>(new TenantFormController().HtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlRepresentation_CultureInfoNull()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/sample/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.Throws<ArgumentNullException>(() => new TenantFormController().XhtmlRepresentation(null as CultureInfo));

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantForm>(new
                {
                    urn = "urn://example.com/form/test",
                    xml = "<container><form /></container>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/sample/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsAssignableFrom<ViewResult>(new TenantFormController().XhtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_Post_TenantFormNull()
        {
            Assert.Throws<ArgumentNullException>(() => new TenantFormController().Post(null as TenantForm));
        }

        [Fact]
        public void op_Post_TenantForm()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/example/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Token token = Token.New();
                TenantForm model = new TenantForm
                {
                    Token = token,
                    Form = new Form()
                };

                SeeOtherResult result = new TenantFormController().Post(model) as SeeOtherResult;

                Assert.Equal<string>("/example/test/form?" + token, result.Location);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_Post_TenantForm_whenResultsExists()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example/test/form").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/example/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Token token = Token.New();
                TenantForm model = new TenantForm
                {
                    Results = new FormSubmission
                    {
                        Token = token
                    }
                };

                SeeOtherResult result = new TenantFormController().Post(model) as SeeOtherResult;

                Assert.Equal<string>("/example/test/submission?" + token, result.Location);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }
    }
}